********************************************************************************
** 	TITLE:		CA19_results                                                  ** 	
**	AUTHOR:	    Review version                                                **
**	DATA:       table_tableau12_2019                                          **
**	VERSION:	Stata 16					                                  **
**	DATE:		October 2022  				                                  **
********************************************************************************

* Version control

version 16.0

* Import the dataset

copy https://www.elections.ca/res/rep/off/ovr2019app/51/data_donnees/table_tableau12.csv table_tableau12_2019.csv, replace
type table_tableau12_2019.csv
import delimited table_tableau12_2019.csv

* Rename variables

rename ïprovince province
rename electoraldistrictnamenomdecircon districtname19
rename electoraldistrictnumbernumãrodec districtnumber19
rename candidatecandidat candidate19
rename candidateresidencerãsidenceducan residence19
rename candidateoccupationprofessionduc occupation19
rename votesobtainedvotesobtenus numberofvotes19
rename percentageofvotesobtainedpourcen numberofvotespercent19
rename majoritymajoritã majoritynumber19
rename majoritypercentagepourcentagedem majoritypercent19

* Generate dichotomous variable for each party 

gen lib19 = strpos(candidate19, "Liberal/LibÃ©ral") > 0
gen con19 = strpos(candidate19, "Conservative/Conservateur") > 0
gen ndp19 = strpos(candidate19, "NDP-New Democratic Party/NPD-Nouveau Parti dÃ©mocratique") > 0
gen green19 = strpos(candidate19, "Green Party/Parti vert") > 0
gen bq19 = strpos(candidate19, "Bloc QuÃ©bÃ©cois/Bloc QuÃ©bÃ©cois") > 0

* Generate party variable

gen party19 = 1 if lib19 == 1
replace party19 = 2 if con19 == 1
replace party19 = 3 if ndp19 == 1
replace party19 = 4 if bq19 == 1
replace party19 = 5 if green19 == 1
replace party19 = 88 if party19 == . 

* Generate rank of parties

gsort districtname19 -numberofvotes19
bysort districtname19 : gen rank19 = _n

gen first19 = party19 if rank19 == 1
gen second19 = party19 if rank19 == 2
gen third19 = party19 if rank19 == 3

bysort districtname19 : gen winner19 = first19[1]
bysort districtname19 : gen runnerup19 = second19[2]
bysort districtname19 : gen thirdplace19 = third19[3]

* Generate margins of victory

bysort districtname19 : gen margin19 = numberofvotespercent19[1] - numberofvotespercent19[2]

* Generate effective number of electoral parties

gen sqrvotepr19 = (numberofvotespercent19/100)^2

bysort districtname19 : egen sumsqrvotepr19 = sum(sqrvotepr19)

gen enep19 = 1/sumsqrvotepr19

* Labelling parties

label define party 1 "Liberal Party" 2 "Conservative Party" 3 "New Democratic Party" 4 "Bloc Quebecois" 5 "Green Party" 88 "Other"
label values winner19 runnerup19 thirdplace19 party

* Selecting, ordering, and sorting variables

keep districtnumber19 districtname19 party19 rank19 numberofvotes19 numberofvotespercent19 margin19 enep19 winner19 runnerup19 thirdplace19

order districtnumber19 districtname19 party19 rank19 numberofvotes19 numberofvotespercent19 margin19 enep19 winner19 runnerup19 thirdplace19

sort districtnumber19 rank19

drop if rank19 > 3

* Drop duplicates

duplicates tag districtnumber19, gen(dup)
duplicates drop districtnumber19, force
drop dup

* Merge with CA15_results.dta

merge 1:1 _n using CA15_results.dta

rename _merge _merge15_19

* Generate variable for incumbent party reelection

recode winner15 1=2 2=3 3=1

gen reelected19 = 1 if winner19 == winner15
replace reelected19 = 0 if reelected19!=1

* Create election variable

gen election19 = "2019 Canadian federal election"

* Saving file

save CA1519_results, replace